<!DOCTYPE html>


<html lang="en">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>地理位置geo处理之mysql函数 |  麦田麦穗</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    </head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-mysql/地理位置geo处理之mysql函数"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  地理位置geo处理之mysql函数
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2017/12/01/mysql/%E5%9C%B0%E7%90%86%E4%BD%8D%E7%BD%AEgeo%E5%A4%84%E7%90%86%E4%B9%8Bmysql%E5%87%BD%E6%95%B0/" class="article-date">
  <time datetime="2017-12-01T09:39:29.000Z" itemprop="datePublished">2017-12-01</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">821</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">3 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <blockquote>
<p>目前越来越多的业务都会基于LBS，附近的人，外卖位置，附近商家等等，现就讨论离我最近这一业务场景的解决方案。</p>
</blockquote>
<p>目前已知解决方案有:</p>
<ul>
<li>mysql 自定义函数计算</li>
<li>mysql geo索引</li>
<li>mongodb geo索引</li>
<li>postgresql PostGis索引</li>
<li>redis geo</li>
<li>ElasticSearch</li>
</ul>
<p>本文测试下mysql 函数运算的性能</p>
<span id="more"></span>

<h2 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h2><h3 id="创建数据表"><a href="#创建数据表" class="headerlink" title="创建数据表"></a>创建数据表</h3><pre class="language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>driver<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">unsigned</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>lng<span class="token punctuation">`</span> <span class="token keyword">float</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>lat<span class="token punctuation">`</span> <span class="token keyword">float</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>id<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8<span class="token punctuation">;</span></code></pre>

<h3 id="创建测试数据"><a href="#创建测试数据" class="headerlink" title="创建测试数据"></a>创建测试数据</h3><p>在创建数据之前先了解下基本的地理知识:</p>
<ul>
<li><p><strong>全球经纬度的取值范围为:</strong> 纬度-90<del>90，经度-180</del>180</p>
</li>
<li><p><strong>中国的经纬度范围大约为：</strong> 纬度3.86<del>53.55，经度73.66</del>135.05</p>
</li>
<li><p>北京行政中心的纬度为39.92，经度为116.46</p>
</li>
<li><p>越北面的地方纬度数值越大，越东面的地方经度数值越大</p>
</li>
<li><p>度分转换： 将度分单位数据转换为度单位数据，公式：度=度+分/60</p>
</li>
<li><p>分秒转换： 将度分秒单位数据转换为度单位数据，公式：度 = 度 + 分 / 60 + 秒 / 60 / 60</p>
</li>
</ul>
<p>在纬度相等的情况下：</p>
<ul>
<li>经度每隔0.00001度，距离相差约1米</li>
</ul>
<p>在经度相等的情况下：</p>
<ul>
<li>纬度每隔0.00001度，距离相差约1.1米</li>
</ul>
<h2 id="mysql函数计算"><a href="#mysql函数计算" class="headerlink" title="mysql函数计算"></a>mysql函数计算</h2><pre class="language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">DELIMITER</span> <span class="token comment">//</span>
<span class="token keyword">CREATE</span> <span class="token keyword">DEFINER</span><span class="token operator">=</span><span class="token punctuation">`</span>root<span class="token punctuation">`</span><span class="token variable">@`localhost`</span> <span class="token keyword">FUNCTION</span> <span class="token punctuation">`</span>getDistance<span class="token punctuation">`</span><span class="token punctuation">(</span>
	<span class="token punctuation">`</span>lng1<span class="token punctuation">`</span> <span class="token keyword">float</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span> 
    <span class="token punctuation">,</span>
	<span class="token punctuation">`</span>lat1<span class="token punctuation">`</span> <span class="token keyword">float</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span>
    <span class="token punctuation">,</span>
	<span class="token punctuation">`</span>lng2<span class="token punctuation">`</span> <span class="token keyword">float</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span> 
    <span class="token punctuation">,</span>
	<span class="token punctuation">`</span>lat2<span class="token punctuation">`</span> <span class="token keyword">float</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span>

<span class="token punctuation">)</span> <span class="token keyword">RETURNS</span> <span class="token keyword">double</span>
    <span class="token keyword">COMMENT</span> <span class="token string">'计算2坐标点距离'</span>
<span class="token keyword">BEGIN</span>
	<span class="token keyword">declare</span> d <span class="token keyword">double</span><span class="token punctuation">;</span>
    <span class="token keyword">declare</span> radius <span class="token keyword">int</span><span class="token punctuation">;</span>
    <span class="token keyword">set</span> radius <span class="token operator">=</span> <span class="token number">6371000</span><span class="token punctuation">;</span> <span class="token comment">#假设地球为正球形，直径为6371000米</span>
    <span class="token keyword">set</span> d <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">2</span><span class="token operator">*</span>ATAN2<span class="token punctuation">(</span>SQRT<span class="token punctuation">(</span>SIN<span class="token punctuation">(</span><span class="token punctuation">(</span>lat1<span class="token operator">-</span>lat2<span class="token punctuation">)</span><span class="token operator">*</span>PI<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">180</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span>   
        <span class="token operator">*</span>SIN<span class="token punctuation">(</span><span class="token punctuation">(</span>lat1<span class="token operator">-</span>lat2<span class="token punctuation">)</span><span class="token operator">*</span>PI<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">180</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">+</span>   
        COS<span class="token punctuation">(</span>lat2<span class="token operator">*</span>PI<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">180</span><span class="token punctuation">)</span><span class="token operator">*</span>COS<span class="token punctuation">(</span>lat1<span class="token operator">*</span>PI<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">180</span><span class="token punctuation">)</span>   
        <span class="token operator">*</span>SIN<span class="token punctuation">(</span><span class="token punctuation">(</span>lng1<span class="token operator">-</span>lng2<span class="token punctuation">)</span><span class="token operator">*</span>PI<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">180</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span>   
        <span class="token operator">*</span>SIN<span class="token punctuation">(</span><span class="token punctuation">(</span>lng1<span class="token operator">-</span>lng2<span class="token punctuation">)</span><span class="token operator">*</span>PI<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">180</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>   
        SQRT<span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">-</span>SIN<span class="token punctuation">(</span><span class="token punctuation">(</span>lat1<span class="token operator">-</span>lat2<span class="token punctuation">)</span><span class="token operator">*</span>PI<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">180</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span>   
        <span class="token operator">*</span>SIN<span class="token punctuation">(</span><span class="token punctuation">(</span>lat1<span class="token operator">-</span>lat2<span class="token punctuation">)</span><span class="token operator">*</span>PI<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">180</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span>   
        <span class="token operator">+</span>COS<span class="token punctuation">(</span>lat2<span class="token operator">*</span>PI<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">180</span><span class="token punctuation">)</span><span class="token operator">*</span>COS<span class="token punctuation">(</span>lat1<span class="token operator">*</span>PI<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">180</span><span class="token punctuation">)</span>   
        <span class="token operator">*</span>SIN<span class="token punctuation">(</span><span class="token punctuation">(</span>lng1<span class="token operator">-</span>lng2<span class="token punctuation">)</span><span class="token operator">*</span>PI<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">180</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span>   
        <span class="token operator">*</span>SIN<span class="token punctuation">(</span><span class="token punctuation">(</span>lng1<span class="token operator">-</span>lng2<span class="token punctuation">)</span><span class="token operator">*</span>PI<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">180</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">*</span>radius<span class="token punctuation">;</span>
    <span class="token keyword">return</span> d<span class="token punctuation">;</span>
<span class="token keyword">END</span><span class="token comment">//</span>
<span class="token keyword">DELIMITER</span> <span class="token punctuation">;</span></code></pre>

<h2 id="创建数据python脚本"><a href="#创建数据python脚本" class="headerlink" title="创建数据python脚本"></a>创建数据python脚本</h2><pre class="language-python" data-language="python"><code class="language-python"><span class="token comment"># coding=utf-8</span>
<span class="token keyword">from</span> orator <span class="token keyword">import</span> DatabaseManager<span class="token punctuation">,</span> Model
<span class="token keyword">import</span> logging
<span class="token keyword">import</span> random
<span class="token keyword">import</span> threading

<span class="token triple-quoted-string string">""" 中国的经纬度范围 纬度3.86~53.55，经度73.66~135.05。大概0.00001度差距1米 """</span>

<span class="token comment"># 创建 日志 对象</span>
logger <span class="token operator">=</span> logging<span class="token punctuation">.</span>getLogger<span class="token punctuation">(</span><span class="token punctuation">)</span>
handler <span class="token operator">=</span> logging<span class="token punctuation">.</span>StreamHandler<span class="token punctuation">(</span><span class="token punctuation">)</span>
formatter <span class="token operator">=</span> logging<span class="token punctuation">.</span>Formatter<span class="token punctuation">(</span>
    <span class="token string">'%(asctime)s %(name)-12s %(levelname)-8s %(message)s'</span><span class="token punctuation">)</span>
handler<span class="token punctuation">.</span>setFormatter<span class="token punctuation">(</span>formatter<span class="token punctuation">)</span>
logger<span class="token punctuation">.</span>addHandler<span class="token punctuation">(</span>handler<span class="token punctuation">)</span>
logger<span class="token punctuation">.</span>setLevel<span class="token punctuation">(</span>logging<span class="token punctuation">.</span>DEBUG<span class="token punctuation">)</span>

<span class="token comment"># Connect to the database</span>

config <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
    <span class="token string">'mysql'</span><span class="token punctuation">:</span> <span class="token punctuation">&#123;</span>
        <span class="token string">'driver'</span><span class="token punctuation">:</span> <span class="token string">'mysql'</span><span class="token punctuation">,</span>
        <span class="token string">'host'</span><span class="token punctuation">:</span> <span class="token string">'localhost'</span><span class="token punctuation">,</span>
        <span class="token string">'database'</span><span class="token punctuation">:</span> <span class="token string">'dbtest'</span><span class="token punctuation">,</span>
        <span class="token string">'user'</span><span class="token punctuation">:</span> <span class="token string">'root'</span><span class="token punctuation">,</span>
        <span class="token string">'password'</span><span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span>
        <span class="token string">'prefix'</span><span class="token punctuation">:</span> <span class="token string">''</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>

db <span class="token operator">=</span> DatabaseManager<span class="token punctuation">(</span>config<span class="token punctuation">)</span>
Model<span class="token punctuation">.</span>set_connection_resolver<span class="token punctuation">(</span>db<span class="token punctuation">)</span>


<span class="token keyword">class</span> <span class="token class-name">Driver</span><span class="token punctuation">(</span>Model<span class="token punctuation">)</span><span class="token punctuation">:</span>
    __table__ <span class="token operator">=</span> <span class="token string">'driver'</span>
    __timestamps__ <span class="token operator">=</span> <span class="token boolean">False</span>
    <span class="token keyword">pass</span>


<span class="token keyword">def</span> <span class="token function">ins_driver</span><span class="token punctuation">(</span>thread_name<span class="token punctuation">,</span>nums<span class="token punctuation">)</span><span class="token punctuation">:</span>
    logger<span class="token punctuation">.</span>info<span class="token punctuation">(</span><span class="token string">'开启线程%s'</span> <span class="token operator">%</span> thread_name<span class="token punctuation">)</span>
    <span class="token keyword">for</span> _ <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>nums<span class="token punctuation">)</span><span class="token punctuation">:</span>
        lng <span class="token operator">=</span> <span class="token string">'%.5f'</span> <span class="token operator">%</span> random<span class="token punctuation">.</span>uniform<span class="token punctuation">(</span><span class="token number">73.66</span><span class="token punctuation">,</span> <span class="token number">135.05</span><span class="token punctuation">)</span>
        lat <span class="token operator">=</span> <span class="token string">'%.5f'</span> <span class="token operator">%</span> random<span class="token punctuation">.</span>uniform<span class="token punctuation">(</span><span class="token number">3.86</span><span class="token punctuation">,</span> <span class="token number">53.55</span><span class="token punctuation">)</span>

        driver <span class="token operator">=</span> Driver<span class="token punctuation">(</span><span class="token punctuation">)</span>
        driver<span class="token punctuation">.</span>lng <span class="token operator">=</span> lng
        driver<span class="token punctuation">.</span>lat <span class="token operator">=</span> lat
        driver<span class="token punctuation">.</span>save<span class="token punctuation">(</span><span class="token punctuation">)</span>

thread_nums <span class="token operator">=</span> <span class="token number">10</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>thread_nums<span class="token punctuation">)</span><span class="token punctuation">:</span>
    t <span class="token operator">=</span> threading<span class="token punctuation">.</span>Thread<span class="token punctuation">(</span>target<span class="token operator">=</span>ins_driver<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">400000</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    t<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<p><img src="http://upload-images.jianshu.io/upload_images/4033700-dda526bdfcc5c759.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>以上脚本创建10个线程，10个线程插入4万条数据。耗费150.18s执行完,总共插入40万条数据</p>
<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><ul>
<li>测试环境</li>
</ul>
<p>系统：mac os</p>
<p>内存：16G</p>
<p>cpu: intel core i5</p>
<p>硬盘: 500g 固态硬盘</p>
<p>测试下查找距离(134.38753,18.56734)这个坐标点最近的10个司机</p>
<pre class="language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token operator">*</span><span class="token punctuation">,</span><span class="token punctuation">`</span>getDistance<span class="token punctuation">`</span><span class="token punctuation">(</span><span class="token number">134.38753</span><span class="token punctuation">,</span><span class="token number">18.56734</span><span class="token punctuation">,</span><span class="token punctuation">`</span>lng<span class="token punctuation">`</span><span class="token punctuation">,</span><span class="token punctuation">`</span>lat<span class="token punctuation">`</span><span class="token punctuation">)</span> <span class="token keyword">as</span> dis <span class="token keyword">from</span> driver <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> dis <span class="token keyword">limit</span> <span class="token number">10</span></code></pre>
<ul>
<li>耗时：18.0s</li>
<li>explain:全表扫描</li>
</ul>
<p>我测试了从1万到10万间隔1万和从10万到90万每间隔10万测试的结果变化</p>
<p><img src="http://upload-images.jianshu.io/upload_images/4033700-c40f60c1ef7b3f18.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h2 id="结论"><a href="#结论" class="headerlink" title="结论"></a>结论</h2><ul>
<li>此方案在数据量达到3万条查询耗时就会超过1秒</li>
<li>大约每增加1万条就会增加0.4秒的耗时</li>
</ul>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          Donate
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://visonforcoding.xyz/2017/12/01/mysql/%E5%9C%B0%E7%90%86%E4%BD%8D%E7%BD%AEgeo%E5%A4%84%E7%90%86%E4%B9%8Bmysql%E5%87%BD%E6%95%B0/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/mysql/" rel="tag">mysql</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2018/07/22/docker-compose-nginx-php/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            docker+compose+nginx+php
          
        </div>
      </a>
    
    
      <a href="/2017/12/01/%E5%9C%B0%E7%90%86%E4%BD%8D%E7%BD%AE%E5%A4%84%E7%90%86%E4%B9%8Bmongodb/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">数据|地理位置geo处理之mongodb geo 索引</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2022
        <i class="ri-heart-fill heart_icon"></i> vison
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="麦田麦穗"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/%E6%97%85%E8%A1%8C/">旅行</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://shenyu-vip.lofter.com">摄影</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/null">Gallery</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>Buy me a cup of coffee~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpeg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wallet.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>